//约瑟夫环问题

#include <stdio.h>

int josephus(int n, int m)
{
    int num[n];
    for(int i = 0; i < n; i++)
    {
        num[i] = 1;
    }

    int count = n;  //幸存者人数
    int flag = 0;    //当前报数位置
    int step = 0;   //当前的报数

    while(count > 1)
    {
        if(num[flag] == 1)
        {
            step++;
            if(step == m)
            {
                num[flag] = 0;
                step = 0;
                count--;
            }
        }
        flag = (flag + 1) % n;
    }

    for(int i = 0; i < n; i++)
    {
        if(num[i] == 1)
        {
            return i + 1;
        }
    }
    return -1;
}

int main()
{
    int n, m;
    printf("请输入总战俘数和死亡报数\n");
    scanf("%d %d", &n, &m);

    int lastPerson = josephus(n, m);
    printf("最后一个人的位置是：%d\n", lastPerson);

    return 0;
}